import { useAppStore } from 'src/stores/app-store';
import { RouteLocationNormalizedLoaded, Router } from 'vue-router';
import { getUrlParam } from 'src/boot/paramhandler';
import { isIOS, isMobile } from 'mobile-device-detect';
import { useWxHook } from './wx_hooks';

export const useLoginHook = (router: Router, route: RouteLocationNormalizedLoaded) => {
  return {
    afterLoginHandler: () => {
      const { pageType, isAuth, token, userInfo, channelCode } = useAppStore();
      const { isAnyirongMini } = useWxHook()

      if (pageType === 'download' && !isAnyirongMini()) {
        if (!isMobile || isIOS) {
          return router.push({ path: '/prodList', query: route.query });
        }
        return router.push({ path: '/down', query: route.query });
      }
      if (pageType == 'list' && !isAnyirongMini()) {
        return router.push({ path: '/prodList', query: route.query });
      }

      // if (!isAuth) {
      //   return router.replace({ path: '/realNameAuth', query: route.query });
      // }

      if (!isAuth || !+userInfo.sourceFlag) {
        if (useWxHook().isMiniprogram.value) {
          return useWxHook().toWxWebview(
            `${location.origin}/infoInput?ch=${channelCode}&token=${token}`,
            true,
          );
        }
        return router.push({ path: '/infoInput', query: route.query });
      }

      if (isAnyirongMini()) {
        // if (useWxHook().isMiniprogram.value) {
        //   return useWxHook().toWxMiniGZH();
        // }
        // return router.replace({ path: '/wxfollow', query: route.query });
        return router.push({ path: '/waitPage', query: route.query });
      }
      if (pageType === 'auth') {
        return router.push({ path: '/waitPage', query: route.query });
        // return router.push({ path: '/infoInput', query: route.query });
      }
      if (pageType === 'auth_download') {
        if (!isMobile || isIOS) {
          return router.push({ path: '/prodList', query: route.query });
        }
        return router.push({ path: '/down', query: route.query });
      }
      return router.push({ path: '/waitPage', query: route.query });
      // if (pageType === 'auth_list') {
      //   return router.replace({ path: '/prodList', query: route.query });
      // }
      // return router.replace({ path: '/prodList', query: route.query });
    },
  };
};
